package com.neusoft.student.dao.impl;

import com.neusoft.student.dao.BaseDao;
import com.neusoft.student.dao.StudentDao;
import com.neusoft.student.model.Student;
import com.neusoft.student.utils.ClassUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: duan
 * @Date: Create in 10:07 2020/10/21
 */
public class StudentDaoImpl extends BaseDao<Student> implements StudentDao {
    @Override
    public Student login(String account, String password) {
        String sql = "select * from student where username=? and password=?";

        return query(sql, Student.class, null, account, password);
    }

    @Override
    public List<Student> queryList(String search) {
        String sql = "select * from student where username = ?";
        List<Student> list = new ArrayList<>();
        query(sql, Student.class, list, search);

        return list;
    }

    @Override
    public List<Student> queryList() {
        String sql = "select * from student";
        List<Student> list = new ArrayList<>();
        query(sql, Student.class, list);

        return list;
    }

    @Override
    public List<Student> queryListByPage(int offset, int limit) {
        String sql = "select * from student limit " + offset + "," + limit;
        List<Student> list = new ArrayList<>();
        query(sql, Student.class, list);

        return list;
    }

    @Override
    public int getCount() {
        String sql = "select count(1) from student";

        return getCount(sql);
    }

    @Override
    public boolean insert(Student bean) {
        System.out.println("StudentDaoImpl:::insert");
//        String sql = "insert into student(id, username, password, sex, phone, dormitory) values(?, ?, ?, ?, ?, ?)";
        //insert into student(id,username,password,sex,phone,dormitory) values('?','?','?','?','?','?')
        String sql = ClassUtil.generateInsertSqlFromObject(bean, "student");

        return doIUD(sql, bean.getId(), bean.getUsername(), bean.getPassword(), bean.getSex(), bean.getPhone(), bean.getDormitory());
    }

    @Override
    public boolean update(Student bean, Integer id) {
        System.out.println("StudentDaoImpl:::update");
        String sql = "update student set username=?, password=?, sex=?, phone=?, dormitory=? where id = ?";
        return doIUD(sql, bean.getUsername(), bean.getPassword(), bean.getSex(), bean.getPhone(), bean.getDormitory(), bean.getId());
    }

    @Override
    public boolean delete(Integer id) {
        String sql = "delete from student where id = ?";

        return doIUD(sql, id);
    }

    @Override
    public Student getById(Integer id) {
        String sql = "select * from student where id = ?";

        return query(sql, Student.class, null, id);
    }
}
